package com.boot.demo.config.dao;

import java.io.IOException;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration//说明是一个配置文件
public class SessionFactoryConfiguration{
	//1.注入dataSource
	@Autowired
	private DataSource dataSource;
	
	//2.定义需要用到的变量
	//mybatis的配置文件
	private String mybatisConfigFile;
	@Value("${mybatis_config_file}")
	public void setMybatisConfigFile(String mybatisConfigFile) {
		this.mybatisConfigFile = mybatisConfigFile;
	}
	//实体类路径
	@Value("${type_aliases_package}")
	private String typeAliasesPackage;
	//mapper的映射文件路径
	private String mapperPath;
	@Value("${mapper_path}")
	public void setMapperPath(String mapperPath) {
		this.mapperPath = mapperPath;
	}
	
	//3.创建会话工厂  sqlSessionFactory
	@Bean("sqlSessionFactory")
	public SqlSessionFactoryBean createSessionFactory() throws IOException {
		//创建对象
		SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
		//注入dataSource
		sqlSessionFactory.setDataSource(dataSource);
		//配置mybatis的配置文件
		sqlSessionFactory.setConfigLocation(new ClassPathResource(mybatisConfigFile));
		//配置实体类路径
		sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage);
		//配置mapper的映射文件路径了
		PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
		String pathSearchPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;//相当于 classpath:/mapper/**.xml
		sqlSessionFactory.setMapperLocations(pathMatchingResourcePatternResolver.getResources(pathSearchPackage));
		
		return sqlSessionFactory;
	}
}
































//@Configuration
//public class SessionFactoryConfiguration {
//	//先注入dataSource
//	@Autowired
//	private DataSource dataSource;
//	
//	//定义需要用到的变量
//	//mybatis的配置文件
//	private static String mybatisConfigFile;
//	@Value("${mybatis_config_file}")
//	public void setMybatisConfigFile(String mybatisConfigFile) {
//		SessionFactoryConfiguration.mybatisConfigFile = mybatisConfigFile;
//	}
//	//映射的mapper文件路径
//	private static String mapperPath;
//	@Value("${mapper_path}")
//	public void setMapperPath(String mapperPath) {
//		SessionFactoryConfiguration.mapperPath = mapperPath;
//	}
//	//实体类ide路径
//	@Value("${type_alias_package}")
//	private String typeAliasesPackage;
//	
//	@Bean(name="sqlSessionFactory")
//	public SqlSessionFactoryBean createSessionFactory() throws IOException {
//		SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
//		//1.注入dataSource
//		sqlSessionFactory.setDataSource(dataSource);
//		//2.配置mybatis的文件
//		sqlSessionFactory.setConfigLocation(new ClassPathResource(mybatisConfigFile));
//		//3.配置实体类的路径
//		sqlSessionFactory.setTypeAliasesPackage(typeAliasesPackage);
//		//4.配置mapper文件的映射路径
//		PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
//		String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
//		sqlSessionFactory.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
//		
//		return sqlSessionFactory;
//	}
//	
//}
